home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / file-ops.h < prev    next >
C/C++ Source or Header  |  1997-03-07  |  5KB  |  220 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if !defined (octave_file_ops_h)
  24. #define octave_file_ops_h 1
  25.  
  26. #include <string>
  27.  
  28. #ifdef HAVE_SYS_TYPES_H
  29. #include <sys/types.h>
  30. #endif
  31.  
  32. class
  33. file_stat
  34. {
  35. public:
  36.  
  37.   file_stat (const string& n = string (), bool fl = true)
  38.     : file_name (n), follow_links (fl), initialized (false)
  39.       {
  40.     if (! file_name.empty ())
  41.       update_internal ();
  42.       }
  43.  
  44.   file_stat (const file_stat& f) { copy (f); }
  45.  
  46.   file_stat& operator = (const file_stat& f)
  47.     {
  48.       if (this != &f)
  49.     copy (f);
  50.  
  51.       return *this;
  52.     }
  53.  
  54.   ~file_stat (void) { }
  55.  
  56.   void get_stats (bool force = false)
  57.     {
  58.       if (! initialized || force)
  59.         update_internal (force);
  60.     }
  61.  
  62.   void get_stats (const string& n, bool force = false)
  63.     {
  64.       if (n != file_name || ! initialized  || force)
  65.     {
  66.       initialized = false;
  67.  
  68.       file_name = n;
  69.  
  70.       update_internal (force);
  71.     }
  72.     }
  73.  
  74.   // File status and info.  These should only be called for objects
  75.   // that are already properly initialized.
  76.  
  77.   bool is_blk (void) const;
  78.   bool is_chr (void) const;
  79.   bool is_dir (void) const;
  80.   bool is_fifo (void) const;
  81.   bool is_lnk (void) const;
  82.   bool is_reg (void) const;
  83.   bool is_sock (void) const;
  84.  
  85.   ino_t ino (void) const { return fs_ino; }
  86.   dev_t dev (void) const { return fs_dev; }
  87.  
  88.   nlink_t nlink (void) const { return fs_nlink; }
  89.  
  90.   uid_t uid (void) const { return fs_uid; }
  91.   gid_t gid (void) const { return fs_gid; }
  92.  
  93.   off_t size (void) const { return fs_size; }
  94.  
  95.   time_t atime (void) const { return fs_atime; }
  96.   time_t mtime (void) const { return fs_mtime; }
  97.   time_t ctime (void) const { return fs_ctime; }
  98.  
  99. #if defined (HAVE_ST_RDEV)
  100.   dev_t rdev (void) const { return fs_rdev; }
  101. #endif
  102.  
  103. #if defined (HAVE_ST_BLKSIZE)
  104.   long blksize (void) const { return fs_blksize; }
  105. #endif
  106.  
  107. #if defined (HAVE_ST_BLOCKS)
  108.   long blocks (void) const { return fs_blocks; }
  109. #endif
  110.  
  111.   string mode_as_string (void) const;
  112.  
  113.   bool ok (void) const { return initialized && ! fail; }
  114.  
  115.   operator void* () const { return ok () ? (void *) -1 : (void *) 0; }
  116.  
  117.   bool exists (void) const { return ok (); }
  118.  
  119.   string error (void) const { return ok () ? string () : errmsg; }
  120.  
  121.   // Has the file referenced by this object been modified since TIME?
  122.   bool is_newer (time_t time) const { return fs_mtime > time; }
  123.  
  124. private:
  125.  
  126.   // Name of the file.
  127.   string file_name;
  128.  
  129.   // TRUE means follow symbolic links to the ultimate file (stat).
  130.   // FALSE means get information about the link itself (lstat).
  131.   bool follow_links;
  132.  
  133.   // TRUE means we have already called stat.
  134.   bool initialized;
  135.  
  136.   // TRUE means the stat for this file failed.
  137.   bool fail;
  138.  
  139.   // If a failure occurs, this contains the system error text.
  140.   string errmsg;
  141.  
  142.   // file type and permissions
  143.   mode_t fs_mode;
  144.  
  145.   // serial number
  146.   ino_t fs_ino;
  147.  
  148.   // device number
  149.   dev_t fs_dev;
  150.  
  151.   // number of links
  152.   nlink_t fs_nlink;
  153.  
  154.   // user ID of owner
  155.   uid_t fs_uid;
  156.  
  157.   // group ID of owner
  158.   gid_t fs_gid;
  159.  
  160.   // size in bytes, for regular files
  161.   off_t fs_size;
  162.  
  163.   // time of last access
  164.   time_t fs_atime;
  165.  
  166.   // time of last modification
  167.   time_t fs_mtime;
  168.  
  169.   // time of last file status change
  170.   time_t fs_ctime;
  171.  
  172. #if defined (HAVE_ST_RDEV)
  173.   // device number for special files
  174.   dev_t fs_rdev;
  175. #endif
  176.  
  177. #if defined (HAVE_ST_BLKSIZE)
  178.   // best I/O block size
  179.   long fs_blksize;
  180. #endif
  181.  
  182. #if defined (HAVE_ST_BLOCKS)
  183.   // number of 512-byte blocks allocated
  184.   long fs_blocks;
  185. #endif
  186.  
  187.   void update_internal (bool force = false);
  188.  
  189.   void copy (const file_stat&);
  190. };
  191.  
  192. extern int is_newer (const string&, time_t);
  193.  
  194. extern int oct_mkdir (const string&, mode_t);
  195. extern int oct_mkdir (const string&, mode_t, string&);
  196.  
  197. extern int oct_mkfifo (const string&, mode_t);
  198. extern int oct_mkfifo (const string&, mode_t, string&);
  199.  
  200. extern int oct_rename (const string&, const string&);
  201. extern int oct_rename (const string&, const string&, string&);
  202.  
  203. extern int oct_rmdir (const string&);
  204. extern int oct_rmdir (const string&, string&);
  205.  
  206. extern string oct_tempnam (void);
  207.  
  208. extern int oct_umask (mode_t);
  209.  
  210. extern int oct_unlink (const string&);
  211. extern int oct_unlink (const string&, string&);
  212.  
  213. #endif
  214.  
  215. /*
  216. ;;; Local Variables: ***
  217. ;;; mode: C++ ***
  218. ;;; End: ***
  219. */
  220.